using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Unity.Entities;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace Gj
{
  [InitializeOnLoad]
  public class LocalEditor
  {
    #region Environment Settings
    [MenuItem("Gj/Env/Develop Environment")]
    static void DevelopEnvironment()
    {
      ChangeEnvironment("develop");
    }
    [MenuItem("Gj/Env/Pre Environment")]
    static void PreEnvironment()
    {
      ChangeEnvironment("pre");
    }
    [MenuItem("Gj/Env/Production Environment")]
    static void ProductionEnvironment()
    {
      ChangeEnvironment("production");
    }
    [MenuItem("Gj/Env/Local Environment")]
    static void LocalEnvironment()
    {
      ChangeEnvironment("local");
    }
    [MenuItem("Gj/Env/Lan Environment")]
    static void LanEnvironment()
    {
      ChangeEnvironment("lan");
    }
    static private void ChangeEnvironment(string env)
    {
      var envSettings = Path.Combine(Application.absoluteURL, string.Format("settings_{0}.json", env));
      if (!File.Exists(envSettings))
      {
        LogTools.Warn("setting is miss:{0}", envSettings);
        return;
      }
      else
      {
        LogTools.Process("loading setting:{0}", envSettings);
      }

      using (StreamReader sr = new StreamReader(envSettings))
      {
        var settings = Path.Combine(Application.absoluteURL, Project.environmentFile);
        using (StreamWriter sw = new StreamWriter(settings))
        {
          string line = "";
          while ((line = sr.ReadLine()) != null)
          {
            sw.WriteLine(line);
          }
        }
      }
      
    }
    #endregion
    

    #region Build Settings
    [MenuItem("Gj/BuildEnv/Phone/Dev")]
    static void PhoneDevBuild()
    {
      ChangeBuild("phone_dev");
    }
    [MenuItem("Gj/BuildEnv/Phone/Product")]
    static void PhoneProductBuild()
    {
      ChangeBuild("phone_production");
    }
    [MenuItem("Gj/BuildEnv/Pc/Dev")]
    static void PcDevBuild()
    {
      ChangeBuild("pc_dev");
    }
    [MenuItem("Gj/BuildEnv/Pc/Product")]
    static void PcProductBuild()
    {
      ChangeBuild("pc_production");
    }
    static private void ChangeBuild(string env)
    {
      string p = Path.Combine("deploy", env);
      FileTools.CopyFile(p, "./");
    }
    #endregion
  }
}